package com.easy.controller;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.bean.Product;
import com.easy.bean.ProductClass;
import com.easy.bean.ProductComment;
import com.easy.bean.User;
import com.easy.common.CommonResult;
import com.easy.dao.IClassDao;
import com.easy.dao.ICommentDao;
import com.easy.dao.IProductDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductDao iProductDao;
    @Autowired
    private IClassDao iClassDao;
    @Autowired
    private ICommentDao iCommentDao;


    @GetMapping("/all")
    public CommonResult getProducts(
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer size){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del",0)
               .eq("is_shelf",0)
               .orderByDesc("date");
        List<Product> product = iProductDao.selectList(wrapper);

        if (page != null && size != null) {
            // 如果提供了分页参数，则进行分页查询
            IPage<Product> productPage = new Page<>(page, size);
            IPage<Product> resultPage = iProductDao.selectPage(productPage, wrapper);
            // 返回分页结果，包括数据和总记录数
            return CommonResult.success(resultPage.getRecords(), product.size());
        } else {
            // 如果没有分页参数，则查询全部用户
            return CommonResult.success(product, product.size());
        }
    }

    @PostMapping("/add")
    public CommonResult addProduct(@RequestBody Product product) {
        int result = iProductDao.insert(product);
        if (result > 0) {
            return CommonResult.success();
        } else {
            return CommonResult.fail();
        }
    }

    @PutMapping("/edit/{id}")
    public CommonResult editProduct(@PathVariable Integer id,@RequestBody Product product) {
        product.setId(id);
        int result = iProductDao.updateById(product);  // 根据ID更新
        if (result > 0) {
            return CommonResult.success(result);
        } else {
            return CommonResult.fail();
        }
    }

    // 删除
    @PutMapping("/isdel/{id}")
    public CommonResult isDelProduct(@PathVariable int id) {
        Product product = new Product();
        product.setId(id);
        product.setIsDel(1);

        int count = iProductDao.updateById(product);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.fail();
        }
    }
    // 上下架
    @PutMapping("/isshelf/{id}")
    public CommonResult isShelfProduct(@PathVariable int id) {
        Product product = iProductDao.selectById(id);
        product.setId(id);
        // 判断是否获取到了产品
        if (product == null) {
            return CommonResult.fail("商品不存在");
        }
        if(product.getIsShelf() == 0){
            product.setIsShelf(1);
        }else if (product.getIsShelf() ==1){
            product.setIsShelf(0);
        }
        int count = iProductDao.updateById(product);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.fail();
        }
    }

    // 查询下架商品
    @GetMapping("/shelfall")
    public CommonResult getshelfProduct(){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del",0)
                .eq("is_shelf",1)
                .orderByDesc("date");
        List<Product> list = iProductDao.selectList(wrapper);
        return CommonResult.success(list,list.size());
    }



    // 模糊查询
    @GetMapping("/search")
    public CommonResult searchProduct(@RequestParam String keyword,@RequestParam(required = false) Integer sort){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del", 0)
                .eq("is_shelf", 0)
                .like("product_name",keyword)
                .or().like("brand",keyword)
                .or().like("description",keyword);
        List<Product> products = iProductDao.selectList(wrapper);

        // 对商品列表进行二次排序
        //  1、销量 2、评分 3、价格低到高 4、价格高到低
        if(sort==null){
            products.sort(Comparator.comparing(Product::getId).reversed());
        }else if (sort == 1){
            products.sort(Comparator.comparing(Product::getCount).reversed());
        }else if(sort == 2){
            products.sort(Comparator.comparing(Product::getRatingAvg).reversed());
        }else if(sort == 3){
            products.sort(Comparator.comparing(Product::getPrice));
        }else if(sort == 4){
            products.sort(Comparator.comparing(Product::getPrice).reversed());
        }else{
            products.sort(Comparator.comparing(Product::getId).reversed());
        }

        return CommonResult.success(products,products.size());
    }






}
